<template>
  <!-- 检验系统 引用顶部菜单  左侧菜单全局LayoutV1 -->
  <div id="homepage">
    <el-container class="homecontainer">
      <el-header class="homeheader">
        <HomepageMenu :menuList="menuList"></HomepageMenu>
      </el-header>
      <el-main style="padding: 0px">
        <RouterView :key="$route.fullPath + 'labhomepage'" name="labhomepage" />
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, getCurrentInstance, watch } from 'vue';
import { AppStore } from '@/stores/store';
import HomepageMenu from '@/components/HomePageMenu.vue';
import { GetPatTypeList } from '@/utils/BasicDictsHelper';
import { IsEmpty } from '@/utils/common';
import { toTreeArray } from 'xe-utils';

let store = AppStore();

let { proxy }: any = getCurrentInstance();

// 获取模块id
let moduleId = '1871457672441683969';

watch(() => store.userInfo.moduleMenuMap, (n, o) => {
  let moduleIdStr = localStorage.getItem('moduleId');
  if (!IsEmpty(moduleIdStr)) {
    moduleId = moduleIdStr;
  }

  getMenuList();
  GetPatTypeList(); /**获取病人类型基础字典 */
});

// t图表数据
onMounted(() => {
  // let _arr = [];
  // const _obj = store.userInfo.moduleMenuMap;
  // for (const key in _obj) {
  //   if (Object.prototype.hasOwnProperty.call(_obj, key)) {
  //     const element = _obj[key];
  //     Object.assign(_arr, element);
  //   }
  // }
  // let _arrObj = [];
  // const arr = treeToArray(_arrObj, JSON.parse(JSON.stringify(_arr)));

  let moduleIdStr = localStorage.getItem('moduleId');
  if (!IsEmpty(moduleIdStr)) {
    moduleId = moduleIdStr;
  }

  // const aimArr = _arrObj.filter((p) => p.url === proxy.$router.currentRoute.value.path);
  // _arrObj.forEach((m) => {
  // });

  getMenuList();
  GetPatTypeList(); /**获取病人类型基础字典 */
});

/**将属性数组展开为扁平数组 */
function treeToArray(result, tree) {
  tree.forEach(ele => {
    const { children, ...item } = ele;
    if (children && children.length > 0) {
      treeToArray(result, children);
    }
    result.push(item);
  })
}

// 菜单数据
let menuList = reactive([]);


// 获取菜单树型数据
function getMenuList() {
  // 通过权限信息获取菜单
  let moduleMenuMap = store.userInfo.moduleMenuMap;
  if (moduleMenuMap && Object.keys(moduleMenuMap).includes(moduleId)) {
    menuList.push(...moduleMenuMap[moduleId].filter((p) => p.isAssembly === '0' || p.isAssembly === null));
  }

  /**发布事件重新加载左侧快捷按钮 */
  //store.setReloadTools((isNaN(store.isReLoadTools) ? 0 : store.isReLoadTools) + 1);
}

// 点击跳转
function gotoPage(path, index) {
  proxy.$router.push(path);
}
</script>
<style lang="scss" scoped>
#homepage {
  width: calc(100%);
  height: calc(100%);
  background-color: $bg-white;
  .el-container {
    height: 100%;
    .el-header {
      padding: 0 !important;
      height: 50px;
    }
    .el-main {
      padding: 0;
      width: 100%;
      height: calc(100% - 50px);
    }
  }
}
</style>
